<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license         
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Blog_model
 *
 * Model used to retrive webites
 *
 * @package			conlabz CMS
 * @subpackage		Models
 */
class Blog_model extends Base_model {
	function Blog_model( ) {
		parent::Base_model( );

		$this->_TABLES = array( 'Option' => $this->config->item( 'backendpro_table_prefix' ) . 'article' );

		// Cache to store already fetched items
		$this->_CACHE = array( );
		$this->load->module_model( 'article', 'upload_model' );
		
		// Object keyword
		// I wouldn't advise changing this, it could corrupt current
		// articles, the reason for needing this is to stop unserialze
		// errors spamming the log files
		$this->object_keyword = "BeP::Object::";

		log_message( 'debug', 'conlabz CMS : Blog_model class loaded' );
	}

	/**
	 * Get Blogs
	 *
	 * @access public
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getBlogs( $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'blog.id, 
							blog.name, 
							blog.teaser,
							blog.active,
							blog.description' 
						 );
		$this->db->from( "blog" );
		$this->db->where( "website_id", $this->session->userdata['website_id'] );
		if( !is_null( $where ) ) {
			$this->db->where( $where );
		}
		if( !is_null( $limit['limit'] ) ) {
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		$this->db->order_by('blog.name desc'); 
		return $this->db->get( );
	}
	
	/**
	 * Insert new Blog
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function insert( $data = array( ) ) {
		//get user_ref from session object
		$data['user_ref'] = $this->session->userdata( 'id' );
		
		//create new blog entry
		$this->db->set( 'id',          $data['id']          );
		$this->db->set( 'name',        $data['name']        );
		$this->db->set( 'teaser',      $data['teaser']      );
		$this->db->set( 'active',      $data['active']      );
		$this->db->set( 'description', $data['description'] );
		$this->db->set( 'website_id',  $this->session->userdata( 'website_id' ) );

		//insert the new blog in the blog DB
		$this->db->insert( 'blog' );
		
		return $this->db->insert_id( );
	}
	
	/**
	 * Update Blog
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function update( $data = array( ), $id ) {		
		$blog = array( );
		$blog['name']        = $data['name'];
		$blog['active']      = $data['active'];
		$blog['teaser']      = $data['teaser'];
		$blog['description'] = $data['description'];

		//insert the new blog
		$this->db->update( 'blog', $blog, array( 'id' => $id ) );
	}
	
	/************************************************* ENTRY SECTION *************************************************/
		
	/**
	 * get Entry ID
	 * 
	 * @return object
	 */
	function _getEntryID( ) {
		$this->db->select( 'blog_entry.entry_id' );
		$this->db->from( "blog_entry, blog" );
		//@todo: needs to be checked, corrected, I think that this should work now
		$this->db->where( "`blog_entry`.`blog_id`=`blog`.`id`" );
		$result = $this->db->get( );
		$row = $result->result_array( );
		return $row[0]['entry_id'];
	}
	
	/**
	 * Get Entries
	 * 
	 * @param $blog_id
	 * @param $where
	 * @param $limit
	 * @return object
	 */
	function getEntries( $blog_id, $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'blog_entry.body,
							blog_entry.title,
							blog_entry.active, 
							blog_entry.teaser, 
							blog_entry.picture,
							blog_entry.user_ref,
							blog_entry.entry_id, 
							blog_entry.timestamp' 
						 );
		$this->db->from( "blog_entry" );
		$this->db->where( "blog_entry.blog_id", $blog_id );
		if( !is_null( $where ) ) {
			$this->db->where( $where );
		}
		if( !is_null( $limit['limit'] ) ) {
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		$this->db->order_by( 'blog_entry.timestamp desc' ); 
		return $this->db->get( );
	}
	
	/**
	 * Get Entries for view file on the frontend
	 * 
	 * @param $blog_id
	 * @param $where
	 * @param $limit
	 * @return object
	 */
	function getEntriesView( $blog_id, $entry_id = NULL ) {
		//get name of the blog
		$this->db->select( 'blog.name' );
		$this->db->from( 'blog' );
		$this->db->where( 'blog.id', $blog_id );
		$blog = $this->db->get( );
		$name = $blog->result_array( );
		$return = array( );
		if( !empty( $name ) ){
			$return['h1'] = $name[0]['name'];
		}
		//get entries
		$this->db->select( 'blog_entry.body,
							blog_entry.title,
							blog_entry.teaser,
							blog_entry.picture,
							blog_entry.entry_id,
							blog_entry.timestamp'
						 );
		$this->db->from( "blog_entry" );
		$this->db->where( "blog_entry.blog_id", $blog_id );
		if( !empty( $entry_id ) ){
			$this->db->where( "blog_entry.entry_id", $entry_id );
		}
		$this->db->where( "blog_entry.active", '1' );
		$this->db->order_by( 'blog_entry.timestamp desc' );
		 
		$entries = $this->db->get( );
		foreach( $entries->result_array( ) as $row ){
			$return['entry'][$row['entry_id']] = array( 'id' => $row['entry_id'],
														'title' => $row['title'],
														'body' => $row['body'],
														'timestamp' => $row['timestamp']
														);
		}
		return $return;
	}
		
	/**
	 * Insert new Entry
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function insert_entry( $data = array( ) ) {		
		//create new entry
		$this->db->set( 'body',      $data['body']                  );
		$this->db->set( 'title',     $data['title']                 );
		$this->db->set( 'teaser',	 $data['teaser']                );
		$this->db->set( 'picture',   $data['picture']               ); 
		$this->db->set( 'blog_id',	 $data['blog_id']               );
		$this->db->set( 'entry_id',  $data['entry_id']              );
		$this->db->set( 'timestamp', $data['timestamp']             ); 
		$this->db->set( 'user_ref',  $this->session->userdata('id') ); 
		
		//insert the new entry
		$this->db->insert( 'blog_entry' );
		
		return $this->db->insert_id( );
	}
	
	/**
	 * Update Blog Entry
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function update_entries( $data = array( ), $entry_id ) {		
		$entry = array( );
		$entry['body']      = $data['body'];
		$entry['title']     = $data['title'];
		$entry['teaser']    = $data['teaser'];
		$entry['active']    = $data['active'];
		$entry['picture']   = $data['picture'];
		$entry['timestamp'] = $data['timestamp']; 
		
		//insert the new entry
		$this->db->update( 'blog_entry', $entry, array( 'entry_id' => $entry_id ) );
	}
	
	function delete_entry( $entry_id ) {
		//delete the blog entry
		$this->db->delete( 'blog_entry', array( 'entry_id' => $entry_id ) );
	}
	
	/**
	 * Constructs an array of the given blogs to be used for a dropdown menu
	 *
	 * @access public
	 * @return array
	 */
	function buildBlogDropdown( ) {
		$blogs = $this->getBlogs( );
		
		$dropdown = array( );
		foreach( $blogs->result( ) as $row ) {
	    	 $dropdown[$row->id] = $row->name;
		}
		
		return $dropdown;
	}
		
	/*function buildPictureDropdown( $opt = false ) {
		$tmp = 0;
		$pic = $this->upload_model->getFolder( $tmp );
		$dropdown = array( );

		if( !empty( $opt ) ) {
			$opt = explode( ",", $opt );
			for( $i = 0; $i < count( $opt ); $i++ ) {
				if( in_array( $opt[$i], array_keys( $pic ) ) ) $dropdown = array_merge( $dropdown, $pic[$opt[$i]] );
			}
		}
		else {
			foreach( $pic as $key => $value ) {
				$dropdown = array_merge( $dropdown, (array)$value );
				//$dropdown[$value->id] = $value->title;
			}
		}
		return $dropdown;
	}	*/
	
	/**
	 * Constructs an array of the given pictures
	 *
	 * @access public
	 * @return array
	 */
	function buildPictureDropdown( ) {
		$tmp = 0;
		$pic = $this->upload_model->getFolder( $tmp );
		
		$dropdown = array( );
		foreach( $pic->result( ) as $row ) {
	    	 $dropdown[$row->id] = $row->title;
		}
	
		return $dropdown;
	}
}
/* End of file blog_model.php */
/* Location: ./modules/blog/models/blog_model.php */